package com.alasbusy.alasbusyapp.activity.twoactivity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;
import com.alasbusy.alasbusyapp.R;
import com.alasbusy.alasbusyapp.activity.pic.PhotoPickerActivity;
import com.alasbusy.alasbusyapp.adapter.ReleaseMsgAdapter;
import com.alasbusy.alasbusyapp.base.BaseActivity;
import com.zhy.android.percent.support.PercentLinearLayout;

import java.util.ArrayList;

public class ReleaseActivity extends BaseActivity {

    private RecyclerView release_recycler;
    private ReleaseMsgAdapter adapter;

    private ArrayList<String> listImagePath;

    private ArrayList<String> mList = new ArrayList<>();
    private ArrayList<String> list = new ArrayList<String>();

    public int listSize = 9;

    @Override
    protected void initView() {
        PercentLinearLayout myself_fragment_title = findViewById(R.id.release_fragment_title);
        setTop(myself_fragment_title);
        getVisibility(true,this);
        getTitle("发布");
        rightVisibility("",R.drawable.release_social_btn_selector);


        release_recycler = findViewById(R.id.release_recycler);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        release_recycler.setLayoutManager(gridLayoutManager);
        adapter = new ReleaseMsgAdapter(ReleaseActivity.this);
        release_recycler.setAdapter(adapter);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        adapter.setOnClickChoosePic(new ReleaseMsgAdapter.OnclickChoosePic() {
            @Override
            public void setOnClickPic() {
                performCodeWithPermission("App请求访问相册权限", new PermissionCallback() {
                    @Override
                    public void hasPermission() {
                        if(list.size() > 0) {
                            listSize = 9 - list.size();
                            if(listSize == 0) {
                                return;
                            }
                        }
                        choosePictrue();
                    }

                    @Override
                    public void noPermission() {
                        //Toasty.warning(context, "读取相册权限异常", Toast.LENGTH_SHORT, true).show();
                        Toast.makeText(ReleaseActivity.this, "读取相册权限异常", Toast.LENGTH_SHORT).show();
                    }
                }, READ_EXTERNAL_STORAGE_permission, WRITE_EXTERNAL_STORAGE_PERMISSION);
            }
        });

        //点击按钮发布消息
        getImageView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }

    @Override
    public int getLayout() {
        return R.layout.activity_release;
    }

    //用户选中图片后，拿到回掉结果
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //Toast.makeText(ReleaseActivity.this, "requestCode == " + requestCode, Toast.LENGTH_SHORT).show();
        if (requestCode == 1001 && resultCode == RESULT_OK) {
            listImagePath = data.getStringArrayListExtra(PhotoPickerActivity.SELECT_RESULTS_ARRAY);
           // compress(listImagePath);
            list.addAll(listImagePath);
            adapter.addMoreItem(listImagePath);
        }
    }

    /*@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1012 && resultCode == RESULT_OK) {
            String resultPhotoUri = data.getStringExtra(PhotoPickerActivity.SELECT_RESULTS);
            if (resultPhotoUri == null) {
                return;
            }
            data.setClass(this, PicCutoutActivity.class);
            startActivityForResult(data, 1002);
        }
    }*/

   /* //压缩 拿到返回选中图片的集合url，然后转换成file文件
    public void compress(ArrayList<String> list) {
        *//*for (String imageUrl : list) {
            //LogUtils.e(">>>>>>", imageUrl);
            File file = new File(imageUrl);
            Toast.makeText(ReleaseActivity.this, "file == " + file.getAbsolutePath(), Toast.LENGTH_SHORT).show();
            //compressImage(file);
        }*//*
        adapter.addMoreItem(list);
    }*/
   /* //压缩
    private void compressImage(File file) {
        Luban.with(this)
                .load(file)
                .ignoreBy(100)
                .setCompressListener(new OnCompressListener() { //设置回调
                    @Override
                    public void onStart() {
                        //TODO 压缩开始前调用，可以在方法内启动 loading UI
                    }

                    @Override
                    public void onSuccess(final File file) {
                        URI uri = file.toURI();
                        String[] split = uri.toString().split(":");
                        list.add(split[1]);//压缩后返回的文件，带file字样，所以需要截取
                        //LogUtils.e(BaseApplication.TAG, uri + "????????????" + split[1]);
                    }

                    @Override
                    public void onError(Throwable e) {
                        //TODO 当压缩过去出现问题时调用
                    }
                }).launch();//启动压缩
    }*/

    private void choosePictrue() {
        Intent intent = new Intent(this, PhotoPickerActivity.class);
        Bundle bundle = new Bundle();
        bundle.putBoolean(PhotoPickerActivity.IS_MULTI_SELECT, true);
        bundle.putStringArrayList(PhotoPickerActivity.SELECT_RESULTS_ARRAY, null);
        bundle.putInt(PhotoPickerActivity.MAX_SELECT_SIZE, listSize);
        intent.putExtras(bundle);
        startActivityForResult(intent, 1001);
    }
}
